package com.mxpio.erp.equipment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.mxpio.erp.common.equipment.entity.EquipmentStockItemInventory;
import com.mxpio.erp.common.equipment.service.EquipmentStockItemInventoryService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 库存信息
 * @Author: MxpIO
 * @Date:   2021-10-13
 * @Version: V1.0
 */
@Service
public class EquipmentStockItemInventoryServiceImpl extends BaseServiceImpl<EquipmentStockItemInventory> implements EquipmentStockItemInventoryService {

    @Override
    public EquipmentStockItemInventory queryExist(String batchNo, String applyItemId, String materialsId) {
        Criteria c = Criteria.create().addCriterion("batch", Operator.EQ, batchNo)
                .addCriterion("inboundApplyItemId", Operator.EQ, applyItemId)
                .addCriterion("materialsId", Operator.EQ, materialsId);
        List<EquipmentStockItemInventory> list = JpaUtil.linq(EquipmentStockItemInventory.class).where(c).list();
        return CollectionUtil.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public Integer queryStockNumByMaterialsId(String materialsId) {
        String sql = "SELECT\n" +
                "            sum( current_onhand_qty ) AS stockNum\n" +
                "        FROM\n" +
                "            tpm_stock_item_inventory\n" +
                "        WHERE\n" +
                "            materials_id = '" + materialsId +"' "+
                "        GROUP BY\n" +
                "            materials_id";
        Integer sum = (Integer) JpaUtil.nativeQuery(sql).getSingleResult();
        return sum;
    }
}
